package com.lebang.controller.sys;

import com.lebang.common.Login;
import com.lebang.domain.ClickStrategy;
import com.lebang.model.Whether;
import com.lebang.service.ClickStrategyService;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;


/**
 * 签到策略
 *
 * @author hwq
 * @date 2020-02-25
 */
@Controller
@RequestMapping("/sys/clickstrategy")
public class ClickStrategyController {
    @Autowired
    private ClickStrategyService baseService;
    @Login
    @GetMapping("/list")
    public String list() {
        return "/clickstrategy/list";
    }
    @Login
    @GetMapping("/add")
    public String add(Long id, Model model) {
        ClickStrategy vo;
        if (id != null) {
            vo = baseService.selectById(id);
        } else {
            vo = new ClickStrategy();
        }
        model.addAttribute("item", vo);
        return "/clickstrategy/edit";
    }

    @Login
    @PostMapping("/edit")
    @ResponseBody
    public JsonData edit(ClickStrategy vo) {
        if (vo.getId() != null) {
            vo.setModifyTime(LocalDateTime.now());
            baseService.updateById(vo);
        } else {
            vo.setCreateTime(LocalDateTime.now());
            vo.setHasDeleted(Whether.NO.getDbIndex());
            baseService.insertModel(vo);
        }
        return Callback.success("success", "修改成功");
    }
    @Login
    @PostMapping("/delete")
    @ResponseBody
    public JsonData delete(ClickStrategy vo) {
        if (vo.getId() != null) {
            vo.setModifyTime(LocalDateTime.now());
            baseService.updateById(vo);
        } else {
            vo.setCreateTime(LocalDateTime.now());
            vo.setHasDeleted(Whether.NO.getDbIndex());
            baseService.insertModel(vo);
        }
        return Callback.success("success", "修改成功");
    }
}
